<?php
include_once dirname(__DIR__).'/third_party/phpcode_shield.php';		//加载防注入代码
// header("Content-Security-Policy: upgrade-insecure-requests");
class Jiang extends CI_Controller
{
    /*
        构造函数
    */
    function __construct()
    {
        header('Content-Type: text/html; charset=utf-8');
        parent::__construct();
        $this->load->helper('url');
    }
    /*
            判断是否微信的
        */
    public function is_weixin()
    {
        if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
            return true;
        }
        return false;
    }

    public function jsonEncodeOptions($array)
    {
        $return_json = json_encode($array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
        return str_replace("'","`",$return_json);
    }


    /*
        提示在微信浏览器打开
    */
    public function warmingWX()
    {
        $data['base_url'] = base_url();
        $this->load->view("warming_wx",$data);
    }

    public function chou(){
        if(isset($_SESSION['WxOpenID'])) {
            $open_id = $_SESSION['WxOpenID'];
            $request_ary['open_id'] = $open_id;
            $this->load->model('account/account_model', '', true);
            $userinfo_result = $this->account_model->getUserInfo($request_ary);

            if (!isset($userinfo_result['data']['account_id']) || $userinfo_result['data']['account_id'] == "") {
                $direct_url = base_url("y/ym");
                Header("Location:" . $direct_url);
                return;
            }
            $ary['account_id'] =$userinfo_result['data']['account_id'];
            $this->load->model('flower/room_model','',true);
            $result = $this->room_model->getRoomTicket($ary);
            $data['ticket_count'] = $result['data']['ticket_count'];

            $data['userinfo'] = $userinfo_result['data'];
            $data['image_url']= Game_Const::ImageUrl;
            $data['base_url'] = base_url();
//var_dump($data['userinfo']);die;
            $setting =  $this->account_model->choujiangSetting();
            foreach ($setting as $item){
                $data['choujiangSetting'][$item['id']] = $item;
            }
            $data['choujiang_journal'] = $this->account_model->choujiangJournal($userinfo_result['data']['account_id'], 14);
            $data['choujiangMouthTicket'] = $this->account_model->choujiangMouthTicket($userinfo_result['data']['account_id']);

//            var_dump($data['choujiangWeekTicket'],date("YW")-1);die;
//            var_dump($data['choujiang_journal']);die;
            $this->load->view("jiang.php", $data);
        }else{
            header("Location:" . base_url() . "y/ym");
        }
    }
    public function chou_result(){
        if(isset($_SESSION['WxOpenID'])) {
            $open_id = $_SESSION['WxOpenID'];
            $request_ary['open_id'] = $open_id;
            $this->load->model('account/account_model', '', true);
            $userinfo_result = $this->account_model->getUserInfo($request_ary);

            if (!isset($userinfo_result['data']['account_id']) || $userinfo_result['data']['account_id'] == "") {
                echo json_encode(["result"=>0, "message"=>"用户登录失效"]);
                exit();
            }
            if ($userinfo_result['data']['choujiang_count']==0 ) {
                echo json_encode(["result"=>0, "message"=>"没有抽奖次数了"]);
                exit();
            }
            $prize_arr = $this->account_model->choujiangSetting();

            //如果中奖数据是放在数据库里，这里就需要进行判断中奖数量
            //在中1、2、3等奖的，如果达到最大数量的则unset相应的奖项，避免重复中大奖
            //code here eg:unset($prize_arr['0'])
            foreach ($prize_arr as $key => $val) {
                $arr[$val['id']] = $val['gailv'];
            }
            $rid = $this->get_rand($arr); //根据概率获取奖项id
            $res = $prize_arr[$rid - 1]; //中奖项
            $choujiang_count = $userinfo_result['data']['choujiang_count'] -1;


            $arr['account_id'] =  Game_CONST::PayAccount;
            $arr['ticket_count'] = $res["ticket_count"];
            $arr['content'] = "";

            if ($res["ticket_count"]) {
                $this->load->model('activity/redenvelop_model', '', true);
                $cresult = $this->redenvelop_model->createRedEnvelopOpt($arr);

                if ($cresult['result'] != 0) {
                    echo json_encode(["result" => 0, "message" => "系统错误1"]);
                    exit();
                }
                $ary['account_id'] = $userinfo_result['data']['account_id'];
                $ary['code'] = $cresult['data']['code'];
                $this->load->model('activity/redenvelop_model', '', true);
                $rresult = $this->redenvelop_model->receiveRedEnvelopOpt($ary);
                if ($rresult['result'] != 0) {
                    echo json_encode(["result" => 0, "message" => "系统错误2"]);
                    exit();
                }
            }
            $this->account_model->choujiangLog(["create_time" => time(), "account_id" => $userinfo_result['data']['account_id'], "ticket_count" => $res['ticket_count'], "chou_income" => 0, "chou_disburse" => 1, "type" => 11, "chou_balance" => $choujiang_count]);
            echo json_encode(['result'=>1,"data"=>$res]);
        }else{
            echo json_encode(["result"=>0, "message"=>"用户登录失效"]);
            exit();
        }
    }

    function get_rand($proArr) {
        $result = '';
        //概率数组的总概率精度
        $proSum = array_sum($proArr);
        //概率数组循环
        foreach ($proArr as $key => $proCur) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $proCur) {
                $result = $key;
                break;
            } else {
                $proSum -= $proCur;
            }
        }
        unset ($proArr);
        return $result;
    }

}